package com.woniuxy.lrz.carlproject.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.lrz.carlproject.common.AppException;
import com.woniuxy.lrz.carlproject.controller.form.formgoods.GoodsSkuAOUForm;
import com.woniuxy.lrz.carlproject.dao.GoodPropertyMapper;
import com.woniuxy.lrz.carlproject.dao.GoodPropertyValueMapper;
import com.woniuxy.lrz.carlproject.model.GoodInfo;
import com.woniuxy.lrz.carlproject.model.GoodProperty;
import com.woniuxy.lrz.carlproject.model.GoodPropertyValue;
import com.woniuxy.lrz.carlproject.model.GoodSku;
import com.woniuxy.lrz.carlproject.dao.GoodSkuMapper;
import com.woniuxy.lrz.carlproject.service.GoodInfoService;
import com.woniuxy.lrz.carlproject.service.GoodSkuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.lrz.carlproject.service.dto.dtogoods.GoodsSkuDto;
import com.woniuxy.lrz.carlproject.service.dto.dtogoods.GoodsSkuPropertyItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品sku表 服务实现类
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@Service
@Slf4j
public class GoodSkuServiceImpl extends ServiceImpl<GoodSkuMapper, GoodSku> implements GoodSkuService {
    @Resource
    private GoodSkuMapper goodSkuMapper;
    @Resource
    private GoodInfoService goodInfoService;
    @Resource
    private GoodPropertyMapper goodPropertyMapper;
    @Resource
    private GoodPropertyValueMapper goodPropertyValueMapper;
    @Override
    public boolean addOrUpdateGoodsSku(GoodsSkuAOUForm goodsSkuAOUForms) throws JsonProcessingException {
        ObjectMapper objectMapper=new ObjectMapper();
        List<GoodSku> skus = goodSkuMapper.selectList(new QueryWrapper<GoodSku>().eq("good_id", goodsSkuAOUForms.getGoodId()));

            HashMap<String, String> skuProperty = (HashMap<String, String>) goodsSkuAOUForms.getSkuProperty();
            String s = objectMapper.writeValueAsString(skuProperty);
            log.info("sku属性:{}",s);
            GoodSku goodSku1 = BeanUtil.copyProperties(goodsSkuAOUForms, GoodSku.class);
            goodSku1.setSkuProperty(s);
            List<GoodSku> collect = skus.stream().filter(goodSku -> goodSku.getSkuProperty().equals(s)).collect(Collectors.toList());
            log.info("sku:{}",collect);
            //验证是新增还是修改
            if(goodsSkuAOUForms.getSkuId()==null){
                //验证新增是否重复
                if(collect.size()>0) throw new AppException("有重名SKU，请稍后再试！");
                //新增
                goodSkuMapper.insert(goodSku1);
                //如果状态是上架，那么商品上架也改为上架
                if(goodSku1.getSkuStatus().equals("ON")){
                    goodInfoService.update(new UpdateWrapper<GoodInfo>().eq("good_id",goodSku1.getGoodId())
                    .set("good_status","UP"));
                }
            }

            //修改
            //验证修改后的sku是否重复
            if(collect.size()>0){
                Integer skuId = collect.get(0).getSkuId();
                if(skuId!=goodSku1.getSkuId()){
                    throw new AppException("有重名SKU，请稍后再试！");
                }
            }
            goodSkuMapper.updateById(goodSku1);

        return true;
    }

    @Override
    public boolean deleteGoodsSku(Integer skuId) {
        //判断是否重复删除
        GoodSku goodSku = goodSkuMapper.selectById(skuId);
        if(goodSku==null) throw new AppException("请勿重复删除！");
        goodSkuMapper.deleteById(skuId);
        return true;
    }

    @Override
    public List<GoodsSkuDto> findGoodsSku(Integer goodId) throws JsonProcessingException {
        ObjectMapper objectMapper=new ObjectMapper();
        List<GoodSku> skus = goodSkuMapper.selectList(new QueryWrapper<GoodSku>().eq("good_id", goodId));
//        List<GoodsSkuDto> goodsSkuDtos = BeanUtil.copyToList(skus, GoodsSkuDto.class);
        //通过商品ID查出对应的分类ID
        ArrayList<GoodsSkuDto> goodsSkuDtos=new ArrayList<>();
        GoodInfo goodInfo = goodInfoService.getById(goodId);
        for (GoodSku goodSku : skus) {
            String skuProperty = goodSku.getSkuProperty();
            HashMap<String,String> map = (HashMap<String,String>)objectMapper.readValue(skuProperty, Map.class);
            log.info("sku:{}",map);
            Set<Map.Entry<String, String>> entries = map.entrySet();
//            ArrayList<GoodsPropertySkuitem> propertyId =new ArrayList<>();
//            ArrayList<GoodsPropertyValueSkuItem> propertyValueId=new ArrayList<>();
            ArrayList<GoodsSkuPropertyItem> goodsSkuPropertyItems=new ArrayList<>();
            for (Map.Entry<String, String> entry : entries) {
                String key = entry.getKey();
                String value = entry.getValue();
                //根据属性名和分类Id查找对应的属性ID
                GoodProperty goodProperty = goodPropertyMapper.selectOne(new QueryWrapper<GoodProperty>().eq("property_name", key)
                        .eq("good_category_id", goodInfo.getGoodCategoryId()));
                //根据属性值和属性Id查找对应的属性值Id
                GoodPropertyValue goodPropertyValue = goodPropertyValueMapper.selectOne(new QueryWrapper<GoodPropertyValue>().eq("property_id", goodProperty.getPropertyId())
                        .eq("propert_value", value));
                GoodsSkuPropertyItem goodsSkuPropertyItem=new GoodsSkuPropertyItem();
                goodsSkuPropertyItem.setPropertyId(goodProperty.getPropertyId());
                goodsSkuPropertyItem.setPropertyName(goodProperty.getPropertyName());
                goodsSkuPropertyItem.setPropertValue(goodPropertyValue.getPropertValue());
                goodsSkuPropertyItem.setPropertyValueId(goodPropertyValue.getPropertyValueId());
                goodsSkuPropertyItems.add(goodsSkuPropertyItem);
//                propertyId.add(goodsPropertySkuitem);
//                propertyValueId.add(goodsPropertyValueSkuItem);
            }
            GoodsSkuDto goodsSkuDto = BeanUtil.copyProperties(goodSku, GoodsSkuDto.class);
            goodsSkuDto.setGoodsSkuPropertyItems(goodsSkuPropertyItems);
            goodsSkuDtos.add(goodsSkuDto);
        }
        return goodsSkuDtos;
    }

    @Override
    public List<GoodsSkuPropertyItem> loadAllPropertys(Integer goodCategoryId) {
        List<GoodsSkuPropertyItem> goodsSkuPropertyItems = goodSkuMapper.loadAllPropertys(goodCategoryId);
        return goodsSkuPropertyItems;
    }
}
